<?php
namespace Admin\Controller;
use Think\Controller;
class DetailController extends BaseController{
	public function showList(){
		$model=M('Detail');
		$count=$model->count(); 
		$page=new \Think\Page($count,5);
		$page->setConfig('prev','上一页');
		$page->setConfig('next','下一页');
		$page->setConfig('last','末页');
		$page->setConfig('first','首页');
		$show=$page->show();
		$data=$model->order('addtime desc')->limit($page->firstRow,$page->listRows)->select();
		$this->assign('data',$data);
		$this->assign('show',$show);
		$this->display();
	}
	public function add(){
		if(IS_POST){
			$post=I('post.');
			$model=D('Detail');
			$result=$model->saveFile($post,$_FILES['file'],$_FILES['thumb']);
			if($result){
				$this->success('添加成功',U('showList'),3);
			}else{
				$this->error('添加失败');

			}
		}else{
			$model=M('Detail');
			$data=$model->select();
			$this->assign('data',$data);
			$this->display();
		}
	}
	public function edit(){
		if(IS_POST){
			$post=I('post.');
			$model=D('Detail');
			$result=$model->saveFile($post,$_FILES['file'],$_FILES['thumb']);
			
			if ($result !== false) {
				$status = array('status'=>'1', 'msg'=>'修改成功');
				$this->ajaxReturn($status);
			}else{
				$status = array('status'=>'0', 'msg'=>'修改失败');
				$this->ajaxReturn($status);
			}
		}else{
			$id=I('get.id');
			$model=M('Detail');
			$data=$model->find($id);
			$this->assign('data',$data);
			$this->display();
		}
	}
	public function del(){
		$id=I('get.id');
		$model=M('Detail');
		$result=$model->delete($id);
		if($result){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}
	//download方法
	public function download(){
		//接收id
		$id = I('get.id');
		//查询数据
		$data = M('Detail') -> find($id);
		//下载代码
		$file = WORKING_PATH . $data['filepath'];
		//输出文件
		header("Content-type: application/octet-stream");
		header('Content-Disposition: attachment; filename="' . basename($file) . '"');
		header("Content-Length: ". filesize($file));
		//输出缓冲区
		readfile($file);
	}
	public function test(){
		$data = array(
            'id'=>2,
            'username'=>'明之暗夜',
            'info'=>array('age'=>24,'address'=>'学府路','url'=>'http://cnblogs.com/dmm888')
        );
        if($data){
            $this->apiReturn(200,'读取用户信息成功',$data);
        }
	}
}